﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using 类型化DataSet重写登陆练习.T_UsersTableAdapters;
using System.Diagnostics;

namespace 类型化DataSet重写登陆练习
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            T_UsersTableAdapter adapter = new T_UsersTableAdapter();
            类型化DataSet重写登陆练习.T_Users.T_UsersDataTable users = adapter.GetDataByUserName(txtUsername.Text);
            if (users.Count <= 0)
            {
                MessageBox.Show("用户名不存在！");
                return;
            }
            类型化DataSet重写登陆练习.T_Users.T_UsersRow user = users[0];
            if (user.ErrorTimes > 3)
            {
                MessageBox.Show("登陆次数过多，请稍后！");
                return;
            }
            if (txtPassword.Text == user.Password)
            {
                adapter.ResetErrorTimesByUserName(txtUsername.Text);
                MessageBox.Show("登陆成功！");
            }
            else
            {
                adapter.IncErrorTimes(txtUsername.Text);
                MessageBox.Show("密码错误！");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            T_UsersTableAdapter adapter = new T_UsersTableAdapter();
            类型化DataSet重写登陆练习.T_Users.T_UsersDataTable users = adapter.GetDataByUserName(txtUsername.Text);
            类型化DataSet重写登陆练习.T_Users.T_UsersRow user = users[0];
            MessageBox.Show(user.ErrorTimes.ToString());
            adapter.IncErrorTimes(txtUsername.Text);
            //和上次一样IncErrorTimesByName修改的是数据库中的数据，本地DataSet中的数据并没有改变
            //如果需要改变，则需要重新更新DataSet
            adapter.Fill(users);
            user = users[0];
            MessageBox.Show(user.ErrorTimes.ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            T_UsersTableAdapter adapter = new T_UsersTableAdapter();
           /* for (int i = 0; i < 3000; i++)
            {
                adapter.Insert(i.ToString(), i.ToString(), 0);
            }*/
            adapter.Connection.Open();
            for (int i = 0; i < 30; i++)
            {
                adapter.Insert(i.ToString(), i.ToString(), 0);
            }
            adapter.Connection.Close();
            sw.Stop();
            MessageBox.Show(sw.Elapsed.ToString());
            //问什么我的电脑没有反应出那么大的效率差呢，基本上相差不了多少....
        }
    }
}
